﻿package com.towker.core.util;

import java.io.File;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 时间操作
 * @author skey
 * @version
 */
public class TimeUtil
{
	/**
	 * 月
	 */
	public static final int Month=1;
	/**
	 * 周
	 */
	public static final int Week=2;
	/**
	 * 日
	 */
	public static final int Day=3;
	/**
	 * 时
	 */
	public static final int Hour=4;
	/**
	 * 分钟
	 */
	public static final int Minute=5;
	
	/**
	 * 返回当前的时间，格式如：2005-12-03 12:03:02
	 * @return String
	 */
	public static String getCurrentTime()
	{
		return getCurrentTime("yyyy-MM-dd HH:mm:ss");
	}

	public static String getCurrentDate()
	{
		return getCurrentTime("yyyy-MM-dd");
	}

	public static long getCurrentTimeValue()
	{
		return Calendar.getInstance().getTimeInMillis();
	}

	public static int getYear()
	{
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.YEAR);
	}

	public static String getCurrentTime(String format)
	{
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(cal.getTime());
	}
	
	/**
	 * 根据毫秒取得时间
	 * 如：10天4时4分
	 * @param millis
	 * @return
	 */
	public static String getTimeByMillis(long millis)
	{
		int day=0;
		int hour=0;
		int min=0;
		int second=0;
		
		int millsPerDay=24*3600*1000;
		
		int millsPerHour=3600*1000;
		
		int millsPerMin=60*1000;
		
		
		
		day=(int)(millis / millsPerDay) ;
		
		hour=(int)((millis -day *millsPerDay)/millsPerHour);
		
		min=(int)((millis-day*millsPerDay-hour * millsPerHour)/millsPerMin);
		
		second=(int)((millis-day*millsPerDay-hour * millsPerHour-min*millsPerMin)/1000);
		
		String str="";
		
		if(day>0)
			str+=day +"天";
		
		if(hour>0)
			str+=hour +"时";
		
		if(min>0)
			str+=min +"分";
		
		if(second>0)
			str+=second +"秒";
		
		return str;
		
	}

	public static String formatDate(Date date, String format)
	{
		try
		{
			if(date == null)
				return "";
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format(date);
		}
		catch(Exception ex)
		{
			return "";
		}
	}

	/**
	 * 将字符串转固定格式的日期类型
	 * @param value
	 * @param format
	 * @return
	 */
	public static Date convertString(String value, String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if (value == null)
			return null;
		try
		{
			return sdf.parse(value);
		}
		catch (Exception e)
		{
			return null;
		}
	}

	/**
	 * 将字符串转日期类型，格式yyyy-MM-dd HH:mm:ss
	 * @param value
	 * @return
	 */
	public static Date convertString(String value)
	{
		return convertString(value, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 将长整形时间转时间类型
	 * @param value
	 * @return 日期型
	 */
	public static Date convertTimeInMillis(long value)
	{
		return new Date(value);
	}

	/**
	 * 将长整形时间按照格式化要求转时间字符串
	 * @param value
	 * @param format
	 * @return
	 */
	public static String convertTimeInMillis(long value, String format)
	{
		Date date = new Date(value);
		return formatDate(date, format);
	}
	
	/**
	 * 将长整形时间转时间字符串，格式为：yyyy-MM-dd HH:mm:ss
	 * @param value
	 * @param format
	 * @return
	 */
	public static String convertDefaultTimeInMillis(long value)
	{
		Date date = new Date(value);
		return formatDate(date, "yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * 时间添加
	 * @param timeUnit
	 * @param amount
	 * @return
	 */
	public static Date dateAdd(int timeUnit,int amount)
	{
		Calendar   rightNow   =   Calendar.getInstance();   
		switch(timeUnit)
		{
			case TimeUtil.Month:
				rightNow.add(Calendar.MONTH, amount);
				break;
			case TimeUtil.Week:
				rightNow.add(Calendar.DAY_OF_MONTH, amount *7);
				break;
			case TimeUtil.Day:
				rightNow.add(Calendar.DAY_OF_MONTH, amount );
				break;
			case TimeUtil.Hour:
				rightNow.add(Calendar.HOUR_OF_DAY, amount );
				break;
			case TimeUtil.Minute:
				rightNow.add(Calendar.MINUTE, amount );
				break;
		}
		//rightNow.add(field, amount)
		return rightNow.getTime();
	}
	
	
	
	 
}
